<%@page pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0" />
		<title>购物车-${initParam.WxAppName}</title>
		<link rel="stylesheet" href="${pageContext.request.contextPath}/bettery/css/life.css?v=192">
		
		<link href="${pageContext.request.contextPath}/bettery/css/reset.css" rel="stylesheet" />
		<link href="${pageContext.request.contextPath}/bettery/css/bootstrap.min.css" rel="stylesheet" />
		<link href="${pageContext.request.contextPath}/bettery/css/app.v2.css" rel="stylesheet" />
		<link href="${pageContext.request.contextPath}/bettery/css/jquery.bxslider.css" rel="stylesheet" />
		<link href="${pageContext.request.contextPath}/bettery/css/style.css" rel="stylesheet" />
		<link href="${pageContext.request.contextPath}/bettery/css/jquery-ui.css" rel="stylesheet" />
		<link rel="stylesheet" href="${pageContext.request.contextPath}/bettery/css/cart.css">
		<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
		<script src="${pageContext.request.contextPath}/bettery/js/jquery-ui-1.10.4.custom.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/bettery/js/jquery.bxslider.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/bettery/js/template.js"></script>
		
		<style type="text/css">
.hasCheck {
	background: #eaeaea;
	border-color: #eaeaea;
	border-radius: 3px 3px 3px 3px;
	color: #FFFFFF;
	cursor: pointer;
	display: inline-block;
	margin: 0;
	overflow: visible;
	padding: 8px 15px;
	text-align: center;
}
.errorMsg{
color: red;
}
</style>
		<script type="text/javascript">
			function checkOne(){
				if($("input[name='gid']:checked").length==0){
					$('#goJieSuan').attr('class','btn btn-default');
					$('#goJieSuan').attr('onclick','return');
					$('#goJieSuan').attr('disabled','disabled');
					$('#checkAll').attr('checked',false);
				}else{
					$('#goJieSuan').attr('class','btn btn-danger');
					$('#goJieSuan').attr('onclick','goJieSuan()');
					$('#goJieSuan').removeAttr('disabled');
					
					var checknum=parseInt($("input[name='gid']:checked").length,10);
				    var allnum=parseInt($("input[name='gid']").length,10);
				    if(allnum==checknum){$('#checkAll').prop('checked',true);}else{$('#checkAll').attr('checked',false);}
				}
			}
			function checkAllGoods(obj){
				if($(obj).is(":checked")){
					$("input[name='gid']").each(function(){$(this).prop("checked",true);});  
					$('#goJieSuan').attr('class','btn btn-danger');
					$('#goJieSuan').attr('onclick','goJieSuan()');
				}else{
					$("input[name='gid']").each(function(){$(this).prop("checked",false);}); 
					$('#goJieSuan').attr('class','hasCheck');
					$('#goJieSuan').attr('onclick','return');
				}
			}
			
			function goJieSuan(){
				var fix="?t=2";
				$(".amount").each(function(){
					var num=$(this).val();
					var goodsId=$(this).attr("goodsId");
					if(goodsId&&num){
						fix=fix+"&ids="+goodsId+"&buyNums="+num;
					}
				});
				if(fix.length>4){
					fix=fix+"&d="+(new Date());
					location.href="queryAddress.do"+fix;
				}
			}
			function removeItem(gid){
				$.ajax({
					url:"removeItem.do?id="+gid,
					dataType:"json",
					success:function(data){
						if(data.success){
							$('#item'+gid).fadeOut(200,function(){
								var totalMoney=$('#totalMoney').attr('total')-0;
								var total=$('#item'+gid).attr('total')-0;
								var now=FloatSub(totalMoney,total);
								$('#item'+gid).remove();
								$("#totalMoney").text("¥"+now).attr('total',now);
								var itemCount=$('#shopcart .item').size();
								if(itemCount==0){
									$('#shopcart').hide();
									$('#empty').show();
								}
								//add begin 2014-2-25
								var totalCount=0;
								totalCount=parseInt($('#cart_count').attr('count'),10)-1;
								//if($('#cart_count').attr('total')){totalCount=parseInt($('#cart_count').attr('total'),10)-1;}
								$('#cart_count').text(totalCount).attr('total',totalCount).attr('count',totalCount);
								//add end
								updateCount();
							});
						}
					}
				});
			}
			function updateCount(){
				var totalCount=0;
				//$('.amount').each(function(){totalCount+=parseInt($(this).val(),10);});
				//$('#cart_count').text(totalCount).attr('total',totalCount);
				
				var totalMoney=0;
				$('.amount').each(function(){
					var tranPrice=$(this).attr("secDoubleNum");
					if(!tranPrice){tranPrice=0.0;}
					totalMoney=FloatAdd(totalMoney,FloatAdd(FloatMul(($(this).val()),($(this).attr('price'))),tranPrice));
				});
				$("#totalMoney").text("¥"+totalMoney.toFixed(2)).attr('total',totalMoney);
			}
			function changeAmount(txt){
				var price=$(txt).attr('price');
				var id=$(txt).attr('cartId');
				var amount=txt.value;
				var maxLimit=$(txt).attr("maxLimit");
				
				var r = /^\+?[1-9][0-9]*$/;　　//正整数 
				if(!r.test(amount)){
					$(txt).val(1);
					amount=1;
				}else{
					if(maxLimit){
						if(parseInt(maxLimit,10)<parseInt(amount,10)){
							$("#errMsg"+id).html("库存量："+maxLimit +" " );
							$(txt).val(maxLimit);amount=maxLimit;
						}
					}
				}
				updateCount();
				var tranPrice=$(txt).attr("secDoubleNum");
				if(!tranPrice){tranPrice=0.0;}
				$(txt).parent().find('b').html("¥"+FloatAdd(FloatMul(price,amount),tranPrice).toFixed(2));
			 	$.ajax({
					url:'changeCount.do?id='+id+"&buyNum="+amount,
					dataType:"json",
					success:function(data){
						if(data.success){
						}
					}
				});
			}
			
		function FloatMul(arg1,arg2){
		var m=0,s1=arg1.toString(),s2=arg2.toString(); 
		try{m+=s1.split(".")[1].length;}catch(e){}
		try{m+=s2.split(".")[1].length;}catch(e){}
		return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
		}   
		
		function FloatAdd(arg1,arg2){
		var r1,r2,m;  
		try{r1=arg1.toString().split(".")[1].length;}catch(e){r1=0;} 
		try{r2=arg2.toString().split(".")[1].length;}catch(e){r2=0;}
		m=Math.pow(10,Math.max(r1,r2)) ; 
		return (arg1*m+arg2*m)/m;
		}
		
		//浮点数减法运算  
		function FloatSub(arg1,arg2){  
		var r1,r2,m,n;  
		try{r1=arg1.toString().split(".")[1].length;}catch(e){r1=0;}  
		try{r2=arg2.toString().split(".")[1].length;}catch(e){r2=0;}  
		m=Math.pow(10,Math.max(r1,r2));  
		//动态控制精度长度  
		n=(r1>=r2)?r1:r2;  
		return ((arg1*m-arg2*m)/m).toFixed(n); 
		}
		</script>
	</head>

	<body>
		<div class="wx_nav">
			<a class="nav_index" href="main.do">购物</a>
			<a class="nav_shopcart on"  href="queryMyCar.do">购物车</a>
			<a class="nav_me"  href="queryUserAddr.do">个人中心</a>
		</div>
		
		<div style="height: 100%" align="center" id='empty'>
			<img src="${pageContext.request.contextPath}/bettery/img/empty.png" style="margin-top: 20%">
			<div
				style="width: 100px; border-top: 1px solid #cacaca; margin-top: 10px; padding-top: 5px;">
				购物车空了
			</div>
		</div>
		<div class='shopcart' id='shopcart' style="display: none;padding-bottom:60px;">
		</div>
		
<script id="cardCar_tmp" type="text/html">
{{each list as value i}}
<div class="item panel" id='item{{value.id}}' total='{{value.totalMony}}'>
	<div class="title panel-heading">
		<i class='icon-trash pull-right' onclick="removeItem({{value.id}})"></i>
	</div>
	<div style="padding: 5px;">
		<div class="goods_icon">
		  <img src="{{value.imgUrl}}" onerror="this.src='${pageContext.request.contextPath}/bettery/img/category.jpg'"  width="50px" height="50px">
		   
		</div>
		<div style="display: inline-block; vertical-align: top;">
			<div>
				<a href='queryById.do?id={{value.goodsId}}'>{{value.name}}</a>
			</div>
			<div style="margin-top: 10px">
				单价：
				<b style="width: 120px; display: inline-block">
				¥{{value.cardprice}}</b>
			</div>
		</div>
	</div>
	<div align="right" class="btns">
		<div style="text-align: right;">
		<span class="errorMsg" limit="100" id="errMsg{{value.id}}">
		</span>
			数量：
			<input type="text" maxlength="5" class='amount' value="{{value.num}}" cartId="{{value.id}}" maxLimit="" 
			price='{{value.cardprice}}' goodsId='{{value.goodsId}}' size="2"	
			onblur="changeAmount(this)" style="width: 40px; height: 24px; display: inline-block; border: 1px solid #cacaca; background: #fff;">
			共：
			<b style="width: 100px; display: inline-block">{{value.totalMony}}</b>
		</div>
	</div>
</div>
{{/each}}

<div class="panel" style="padding: 10px">
	<div align="right">
		总价格：
		<b id='totalMoney' total='{{allTotalMoney}}'>¥{{allTotalMoney}}</b>
		<a class='btn btn-danger' style="text-decoration: none;"
			onclick="goJieSuan()" id="goJieSuan">去结算</a>
	</div>
</div>
</script>
<script>
var json = ${json};
var allTotalMoney=0;
if(json.allTotalMoney){
	allTotalMoney=parseFloat(json.allTotalMoney ,10).toFixed(2);
	if(json.items&&json.items.length>0){
	    var list=json.items;
		for(var i=0;i<list.length;i++){
			if(list[i].cardprice){
				list[i].cardprice=parseFloat(list[i].cardprice,10).toFixed(2);
			}
			if(list[i].totalMony){
				list[i].totalMony=parseFloat(list[i].totalMony,10).toFixed(2);
			}
		}
		var data={list:list,allTotalMoney:allTotalMoney};
		var html = template('cardCar_tmp', data);
		document.getElementById('shopcart').innerHTML = html;
		$('#empty').hide();
	    $("#shopcart").show();
	}
}

</script>
	</body>
</html>
